package com.woniu.defecttracking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.defecttracking.dto.AccountForProjectDto;
import com.woniu.defecttracking.dto.AccountForProjectsDto;
import com.woniu.defecttracking.dto.FindAccountDto;
import com.woniu.defecttracking.entity.Account;
import com.woniu.defecttracking.entity.ProjectAccount;
import com.woniu.defecttracking.mapper.ProjectAccountMapper;
import com.woniu.defecttracking.param.AddProjectAccountParam;
import com.woniu.defecttracking.param.FindAccountParam;
import com.woniu.defecttracking.param.SelAccountParam;
import com.woniu.defecttracking.service.AccountRoleService;
import com.woniu.defecttracking.service.AccountService;
import com.woniu.defecttracking.service.ProjectAccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tiangle
 * @since 2021-12-31
 */
@Service
public class ProjectAccountServiceImpl extends ServiceImpl<ProjectAccountMapper, ProjectAccount> implements ProjectAccountService {
    @Autowired
    private AccountService accountService;
    @Autowired
    private AccountRoleService accountRoleService;

    @Override
    public List<Account> findProjectId(Integer id) {
        List<Integer> projectAccounts = getProjectAccounts(id);
        List<Account> accounts = accountService.listByIds(projectAccounts);
        for (int i = 0; i < accounts.size(); i++) {
            if (accounts.get(i).getAccountIsdel()==1) {
                accounts.remove(i);
                i--;
            }
        }

        return accounts;
    }

    @Override
    public boolean addToProject(AddProjectAccountParam addProjectAccountParam) {
        ArrayList<ProjectAccount> projectAccounts = new ArrayList<>();
        List<Integer> checkAccounts = addProjectAccountParam.getCheckAccounts();
        for (Integer checkAccount : checkAccounts) {
            projectAccounts.add(ProjectAccount.builder().accountId(checkAccount).projectId(addProjectAccountParam.getProjectId()).build());
        }
        QueryWrapper<ProjectAccount> projectAccountQueryWrapper = new QueryWrapper<>();
        projectAccountQueryWrapper.eq("project_id",addProjectAccountParam.getProjectId());
        remove(projectAccountQueryWrapper);
        saveBatch(projectAccounts);
        return true;
    }

    @Override
    public List<Integer> getProjectAccounts(Integer id) {
        QueryWrapper<ProjectAccount> projectAccountQueryWrapper = new QueryWrapper<>();
        projectAccountQueryWrapper.eq("project_id",id);
        List<ProjectAccount> list = list(projectAccountQueryWrapper);

        List<Integer> collect = list.stream().map(a -> a.getAccountId()).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<Integer> getProjects(Integer id) {
        QueryWrapper<ProjectAccount> projectAccountQueryWrapper = new QueryWrapper<>();
        projectAccountQueryWrapper.eq("account_id",id);
        List<ProjectAccount> list = list(projectAccountQueryWrapper);
        List<Integer> collect = list.stream().map(a -> a.getProjectId()).collect(Collectors.toList());
        return collect;
    }

    /**
    * @作者: tiangle
    * @描述：根据project分页查询拥有的account和role
    * @时间：2022/1/7 0:20
    * @参数类型:[selAccountParam]
    * @返回：com.woniu.defecttracking.dto.AccountForProjectsDto
    **/
    @Override
    public AccountForProjectsDto getByProject(SelAccountParam selAccountParam) {
        System.out.println(selAccountParam);
        AccountForProjectsDto accountForProjectsDto = new AccountForProjectsDto();
        Page<ProjectAccount> projectAccountPage = new Page<>();
        projectAccountPage.setCurrent(selAccountParam.getPageIndex());
        projectAccountPage.setSize(selAccountParam.getPageSize());
        QueryWrapper<ProjectAccount> projectAccountQueryWrapper = new QueryWrapper<>();
        projectAccountQueryWrapper.eq("project_id",selAccountParam.getId());
        Page<ProjectAccount> projectAccounts = page(projectAccountPage, projectAccountQueryWrapper);
        accountForProjectsDto.setTotal(projectAccounts.getTotal());
        accountForProjectsDto.setPageIndex(projectAccounts.getCurrent());
        List<AccountForProjectDto> accountForProjectDtos = new ArrayList<>();
        List<Integer> accountId = projectAccounts.getRecords().stream().map(a -> a.getAccountId()).collect(Collectors.toList());
        System.out.println(accountId);
        List<Account> accounts = accountService.listByIds(accountId);
        for (int i = 0; i < accounts.size(); i++) {
            if (accounts.get(i).getAccountIsdel() == 1) {
                accounts.remove(i);
                i--;
            }
        }
        for (Account account : accounts) {
            accountForProjectDtos.add(AccountForProjectDto.builder()
                    .accountId(account.getAccountId())
                    .accountName(account.getAccountName())
                    .roles(accountRoleService.getRoles(account.getAccountId()))
                    .build());

        }

        accountForProjectsDto.setList(accountForProjectDtos);
        return accountForProjectsDto;
    }

    @Override
    public boolean del(Integer projectId, Integer accountId) {
        QueryWrapper<ProjectAccount> projectAccountQueryWrapper = new QueryWrapper<>();
        projectAccountQueryWrapper.eq("project_id",projectId);
        projectAccountQueryWrapper.eq("account_id",accountId);
        remove(projectAccountQueryWrapper);
        return true;

    }
}
